package middlewares

import (
	"strings"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func IsHaveAuthor(c *gin.Context) {

	//1.获取访问的url
	pathName := strings.Split(c.Request.URL.String(), "?")[0]

	//2.获取session中保存的用户信息
	session := sessions.Default(c)
	username := session.Get("username")

	//3.判断session中的用户信息是否存在 不存在则跳转到登录页面
	usernameStr, ok := username.(string)

	if ok {
		c.Set("username", usernameStr)
	} else {
		if pathName != "/admin/login" && pathName != "/admin/doLogin" {
			c.Redirect(302, "/admin/login")
		}
	}

}
